package com.itlong.cloud.controller.old;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.app.old.AppOldUserInfoDto;
import com.itlong.cloud.POJO.VO.app.old.AppSysCodeVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.SmartCommunityEnum;
import com.itlong.cloud.enumerate.oldapp.Code;
import com.itlong.cloud.enumerate.oldapp.Message;
import com.itlong.cloud.equip.app.IAppDeviceServiceClient;
import com.itlong.cloud.project.app.IAppProjectServiceClient;
import com.itlong.cloud.project.app.IAppRoomServiceClient;
import com.itlong.cloud.user.app.IAppOldRegisterServiceClient;
import com.itlong.cloud.user.app.IAppOldServeServiceClient;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <desc>
 *     旧版APP服务版块控制器入口
 * </desc>
 * Created 2017/9/19
 */


@RestController
@RequestMapping("/mobile/interface")
public class AppOldServeController {
    private static final Logger LOG = LoggerFactory.getLogger(AppOldServeController.class);

    @Value("${" + PlatformConstants.PROPERTYMANAGE_WEB_URL + "}")
    public String PROPERTY_MANAGE_WEB_URL;

    @Autowired
    IAppOldServeServiceClient iAppOldServeServiceClient;

    @Autowired
    IAppOldRegisterServiceClient iAppOldRegisterServiceClient;

    @Autowired
    IAppRoomServiceClient iAppRoomServiceClient;

    @Autowired
    IAppProjectServiceClient iAppProjectServiceClient;

    @Autowired
    IAppDeviceServiceClient iAppDeviceServiceClient;
    /**
     * <desc>
     *     旧版APP_获取服务版块的广告
     * </desc>
     * @param type
     * @param phoneNo
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/lifeservice/ads")
    public Message getAd(String type,String phoneNo,String projectId) throws Exception{
        if (StringUtils.isBlank(projectId)){
            projectId = "";
        }
        if (StringUtils.isBlank(type)){
            type = "";
        }
        Message message = iAppOldServeServiceClient.getAd(phoneNo,projectId,type);
        return message;
    }

    /**
     * <desc>
     *     旧版APP_获取用户物业信息列表
     * </desc>
     * @param phoneNo
     * @param projectId
     * @param propertyType 1-通知,2-便民,3-报修,4-投诉
     * @param req
     * @param resp
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/property/propertyInfoList")
    public Message getMyPropertyInfo(String phoneNo,String projectId,String propertyType,
        final HttpServletRequest req, HttpServletResponse resp) throws Exception{
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterServiceClient.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto == null){
            return new Message(Code.NOT_FOUNDUSER);
        }
        String url = PROPERTY_MANAGE_WEB_URL+"easyList?type="+propertyType+"&projectId="+projectId+"&userId="+appOldUserInfoDto.getUserId();
        return  new Message(Code.SUCCESS,url);
    }

    /**
     * <desc>
     *     旧版APP_用户报修
     * </desc>
     * @param phoneNo
     * @param imageStr
     * @param imageType
     * @param propertyInfo
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/property/reportRepair")
    public Message addReportRepair(String phoneNo,String imageStr,String imageType,String propertyInfo,String projectId) throws Exception{
        Message message = iAppOldServeServiceClient.addReportRepairOrComplaint(phoneNo, imageStr, imageType, propertyInfo, projectId, "3");
        return  message;
    }

    /**
     * <desc>
     *     旧版APP_用户投诉
     * </desc>
     * @param phoneNo
     * @param imageStr
     * @param imageType
     * @param propertyInfo
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/property/reportComplaint")
    public Message addReportComplaint(String phoneNo,String imageStr,String imageType,String propertyInfo,String projectId) throws Exception{
        Message message = iAppOldServeServiceClient.addReportRepairOrComplaint(phoneNo, imageStr, imageType, propertyInfo, projectId, "4");
        return  message;
    }

    /**
     * <desc>
     *     旧版APP_获取商圈所有的种类及对应包含的标签
     * </desc>
     * @param businessType
     * @param childType
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/lifeservice/businessinfo/typeLabel")
    public Message getBusinessTypeandLabel(String businessType,String childType) throws Exception{
        List<AppSysCodeVO> sysCodes = new ArrayList<>();
        AppSysCodeVO appSysCodeVO = new AppSysCodeVO("1","11","business_type","1","美食","","1","","1");// 主要用户预防苹果闪退
        sysCodes.add(appSysCodeVO);
        return  new Message(Code.SUCCESS,sysCodes);
    }

    /**
     * <desc>
     *     旧版APP_获取商户信息列表
     * </desc>
     * @param businessType
     * @param serviceLabel
     * @param pageNo
     * @param pageSize
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/lifeservice/businessinfo/list")
    public Message getBusinessList(String businessType,String serviceLabel,Integer pageNo,Integer pageSize,String projectId) throws Exception{
        JSONObject js = new JSONObject();
        js.put("rows",null);
        js.put("totalNumber",0);
        return  new Message(Code.SUCCESS,js);
    }

    /**
     * <desc>
     *     旧版APP_查看商户的详细信息
     * </desc>
     * @param businessId
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/lifeservice/businessinfo/detail")
    public Message getBusinessDetail(String businessId,Integer pageNo,Integer pageSize) throws Exception{
        JSONObject js = new JSONObject();
        js.put("productInfoArr",null);
        js.put("totalNumber",0);
        return  new Message(Code.SUCCESS,js);
    }



    /**
     * <desc>
     *     旧版APP_检测APP版本
     * </desc>
     * @param type android/ios/iossdk/androidsdk
     * @param sort test/official
     * @return
     */
    @ResponseBody
    @RequestMapping(path = "/app/version/latest")
    public Message getLatestApp(String type,String sort){
        Message message = iAppOldServeServiceClient.getLatestApp(type,sort);
        return  message;
    }

    /**
     * <desc>
     *     旧版APP_工程接口_获取设备机号列表
     * </desc>
     * @param projectId
     * @param deviceName
     * @param deviceType
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/engineer/getDeviceIdByEngineer")
    public Message getDeviceIdByEnginerr(String projectId,String deviceName,String deviceType) throws Exception{
        return  new Message(Code.SUCCESS,null);
    }

    /**
     * <desc>
     *     旧版APP_工程接口_获取项目密钥
     * </desc>
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/engineer/getProjectAuthInfo")
    public Message getProjectAuthInfo(String projectId) throws Exception {
        return  new Message(Code.SUCCESS,null);
    }

    /**
     * <desc>
     *     旧版APP_工程接口_设备阈值上传
     * </desc>
     * @param deviceUnique
     * @param signal
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/engineer/uploadEngineerData")
    public Message uploadEngineerData(String deviceUnique,String signal,String projectId) throws Exception{

        return  new Message(Code.SUCCESS);
    }

    /**
     * <desc>
     *     旧版APP_工程接口_开门首页
     * </desc>
     * @param pushAutoIndex
     * @param phoneNo
     * @param pageSize
     * @param pageNo
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/engineer/projectOpen")
    public Message appProjectOpen(String pushAutoIndex, String phoneNo,Integer pageSize, Integer pageNo,String projectId)throws Exception {
        JSONObject data = new JSONObject();
        data.put("pushInfo", null);
        data.put("ads",null);
        data.put("deviceInfos", null);
        return new Message(Code.SUCCESS, data);
    }

    /**
     * <desc>
     *     旧版APP_云对讲_业主设置开门密码
     * </desc>
     * @param phoneNo
     * @param projectId
     * @param doorPwd
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/device/setDoorPwd")
    public Message setDoorPwd(String phoneNo,String projectId,String doorPwd) throws Exception{
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterServiceClient.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto == null){
            return new Message(Code.NOT_FOUNDUSER);
        }
        iAppRoomServiceClient.setOpenPwd(projectId,appOldUserInfoDto.getUserId(),doorPwd);
        return  new Message(Code.SUCCESS);
    }

    /**
     * <desc>
     *     旧版APP_云对讲_点击设置房间电话是否允许呼叫
     * </desc>
     * @param phoneNo
     * @param projectId
     * @param isAllowPhone
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/device/setAllowPhone")
    public Message setAllowPhone(String phoneNo,String projectId,String isAllowPhone) throws Exception{
        AppOldUserInfoDto appOldUserInfoDto = iAppOldRegisterServiceClient.selectDtoByPhone(phoneNo);
        if (appOldUserInfoDto == null){
            return new Message(Code.NOT_FOUNDUSER);
        }
        isAllowPhone=isAllowPhone.equals("0") ? "1" : "0";
        iAppProjectServiceClient.setAllowPhone(projectId, appOldUserInfoDto.getUserId(), isAllowPhone);
        //推送设备拉取业主信息
        iAppDeviceServiceClient.pushCloudDeviceAuth(projectId,appOldUserInfoDto.getUserId(),
                SmartCommunityEnum.PUSH_ROOM.getCommandType());
        return  new Message(Code.SUCCESS);
    }




















}
